package com.Pure;


import android.view.KeyEvent;
import android.view.MotionEvent;
import com.e3roid.E3Activity;
import com.e3roid.E3Scene;
import com.e3roid.drawable.Menu;
import com.e3roid.drawable.Shape;
import com.e3roid.drawable.Sprite;
import com.e3roid.drawable.modifier.*;

import com.e3roid.drawable.modifier.function.*;
import com.e3roid.drawable.texture.Texture;
import com.e3roid.drawable.texture.AssetTexture;

public class PureMenu extends Menu {
	
	//Background Sprite
	 Sprite menuBK;
	 Texture texMenuBk;
	 //Texture texAlphaBlack;

	 public PureMenu(E3Activity main,E3Scene scene)
	 {
		 super();
		 
		 texMenuBk = new AssetTexture("MenuBk.png",main);
		 menuBK = createMenuBackground(texMenuBk);
		 
		 //texAlphaBlack = new AssetTexture("AlphaBlack.png",main);
		 //Sprite AlphaBlackSprite = new Sprite(texAlphaBlack,0,0);
		 //AlphaBlackSprite.setAlpha(1);
		 //addEventListener to menuBK and enable updated
		 scene.addEventListener(menuBK);
		 scene.setBackgroundColor(0f, 0f, 0f, 1);
         add(menuBK);
         //add(AlphaBlackSprite);
         
	 }
	 
	 @Override
	 public void onRemove()
	 {
		 
		 super.onRemove();
	 }
	 public Sprite createMenuBackground(Texture texMenuBk)
	    {
	    	Sprite menuBK = new Sprite(texMenuBk,0,0){
				@Override
				public boolean onTouchEvent(E3Scene arg0, Shape arg1,
						MotionEvent arg2, int arg3, int arg4) {
						
					if (arg2.getAction() == MotionEvent.ACTION_DOWN) {
						
						//Modifier is make something changing according the time.
						ShapeModifier modifier =  new ProgressModifier(AlphaModifier.getFadeOutModifier(1), 300L, new AcceleroIn(10),  this)  ;
						modifier.passValue(arg0);
						this.addModifier(modifier);
						
						
						return true;
					}
					return false;
				}
				//when finished fade out effect , hide the menu.
				@Override
				public void onModifierFinished(ShapeModifier modifier, Shape shape) {
					E3Scene scene = (E3Scene)modifier.receiveValue();
					scene.setBackgroundColor(0f, 0f, 0f, 1);
					scene.hideMenu();
					scene.resume();
					
				}
				
				
			};
	        return menuBK;
	    }

	    
		public boolean onKeyDown(E3Scene scene, int keyCode, KeyEvent event) {
	    	
			if(keyCode == KeyEvent.KEYCODE_MENU && event.getAction() == KeyEvent.ACTION_DOWN) {
				if (scene.isMenuVisible()) {
					menuBK.setAlpha(1);
					scene.hideMenu();
					scene.resume();
					return true;
				} else {
					menuBK.setAlpha(1);
					scene.showMenu();
					scene.pause();
					return true;
				}
				
			}
			return false;
		}
}
